package org.csu.mypetstore.api.controller.front;


import org.csu.mypetstore.api.common.CommonResponse;
import org.csu.mypetstore.api.common.ResponseCode;
import org.csu.mypetstore.api.entity.Account;
import org.csu.mypetstore.api.service.AccountService;
import org.csu.mypetstore.api.vo.AccountVO;
import org.csu.mypetstore.api.vo.ItemVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
@RequestMapping("/account/")
public class AccountController {


    @Autowired
    private AccountService accountService;



    @PostMapping("login")
    @ResponseBody
    public CommonResponse<AccountVO> login(
            @RequestParam String username,
            @RequestParam String password,
            HttpSession session){
        CommonResponse<AccountVO> response = accountService.getAccount(username,password);
        if(response.isSuccess()){
            session.setAttribute("login_account" , response.getData());
        }
        return response;
    }

    @PostMapping("get_login_account_info")
    @ResponseBody
    public CommonResponse<AccountVO> getLoginAccountInfo(HttpSession session){
        AccountVO loginAccount =(AccountVO)session.getAttribute("login_account");
        if(loginAccount != null){
            return CommonResponse.createForSuccess(loginAccount);
        }
        return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), "用户未登录，不能获取用户信息");
        //return CommonResponse.createForError("用户未登录，不能获取用户信息");
    }

    @GetMapping("{id}/get_uer_details")
    @ResponseBody
    public CommonResponse<AccountVO> getUserDetails(@PathVariable("id") String username){
        return accountService.getAccount(username);
    }

    @PostMapping("update-password")
    @ResponseBody
    public CommonResponse<AccountVO> getUpdataPassword(@RequestParam String username,
                                                       @RequestParam String password,
                                                       HttpSession session){
       return accountService.getUpdataPassword(username,password);
    }

    @PostMapping("update-userAll")
    @ResponseBody
    public CommonResponse<AccountVO> getUpdataUserAll(@RequestBody Map<String,Object> map, HttpSession session){
        AccountVO accountVO = new AccountVO();
        System.out.println(map);
        accountVO.setUsername(map.get("username").toString());
        accountVO.setPassword(map.get("password").toString());
        accountVO.setEmail(map.get("email").toString());
        accountVO.setFirstname(map.get("firstname").toString());
        accountVO.setLastname(map.get("lastname").toString());
        accountVO.setStatus(map.get("status").toString());
        accountVO.setAddr1(map.get("addr1").toString());
        accountVO.setAddr2(map.get("addr2").toString());
        accountVO.setCity(map.get("city").toString());
        accountVO.setState(map.get("state").toString());
        accountVO.setZip(map.get("zip").toString());
        accountVO.setCountry(map.get("country").toString());
        accountVO.setPhone(map.get("phone").toString());
        accountVO.setFavouciteCategoryId(map.get("favouciteCategoryId").toString());
        accountVO.setBannername(map.get("bannername").toString());
        accountVO.setLangpref(map.get("langpref").toString());
        accountVO.setFavcategory(map.get("favcategory").toString());
        accountVO.setMylistopt((boolean)map.get("mylistopt"));
        accountVO.setBanneropt((boolean)map.get("banneropt"));


        System.out.println(accountVO);
        return accountService.getUpdataUserAll(accountVO);
    }


    @PostMapping("register")
    @ResponseBody
    public CommonResponse<AccountVO> getRegister(@RequestBody Map<String,Object> map, HttpSession session){
        AccountVO accountVO = new AccountVO();
        System.out.println(map);
        accountVO.setUsername(map.get("username").toString());
        accountVO.setPassword(map.get("password").toString());
        accountVO.setEmail(map.get("email").toString());
        accountVO.setFirstname(map.get("firstname").toString());
        accountVO.setLastname(map.get("lastname").toString());
        accountVO.setStatus(map.get("status").toString());
        accountVO.setAddr1(map.get("addr1").toString());
        accountVO.setAddr2(map.get("addr2").toString());
        accountVO.setCity(map.get("city").toString());
        accountVO.setState(map.get("state").toString());
        accountVO.setZip(map.get("zip").toString());
        accountVO.setCountry(map.get("country").toString());
        accountVO.setPhone(map.get("phone").toString());
        accountVO.setFavouciteCategoryId(map.get("favouciteCategoryId").toString());
        accountVO.setBannername(map.get("bannername").toString());
        accountVO.setLangpref(map.get("langpref").toString());
        accountVO.setFavcategory(map.get("favcategory").toString());
        accountVO.setMylistopt((boolean)map.get("mylistopt"));
        accountVO.setBanneropt((boolean)map.get("banneropt"));
        System.out.println(accountVO);
        return accountService.getRegister(accountVO);
    }

}
